package com.idp.web.test.service.impl;

import com.idp.web.test.service.OutService;
import com.idp.web.test.dao.DetailListDao;
import com.idp.web.test.dao.OutDao;
import com.idp.web.test.dao.ProductDao;
import com.idp.web.test.dao.StockDao;
import com.idp.web.test.entity.DetailList;
import com.idp.web.test.entity.Out;
import com.idp.web.test.entity.Stock;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.idp.common.persistence.Page;

/**
 * 
 * 1111111service实现类
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Service("outService")
@Transactional
public class OutServiceImpl implements OutService {

	@Resource
	private OutDao outDao;
	@Resource
	private DetailListDao detailListDao;
	@Resource
	private StockDao stockDao;
	@Resource
	private ProductDao productDao;

	@Override
	public Page<Out> findByPage(Out out, Page<Out> page) {

		page.setResult(outDao.find(out, page));

		return page;
	}

	@Override
	public List<Out> findBySearch(Out out) {

		return outDao.find(out);
	}

	@Override
	public Out getById(Integer id) {

		return outDao.getById(id);
	}

	@Override
	public void add(Out out) {
		outDao.add(out);
	}

	@Override
	public void update(Out out) {

		outDao.update(out);
	}

	@Override
	public void delete(Integer id) {

		outDao.delete(id);
	}

	@Override
	public Page<Out> sreachByWarehouseAndOutStatus(Out out, Page<Out> page) {
		// TODO Auto-generated method stub
		page.setResult(outDao.sreachByWarehouseAndPutStatus(out, page));
		return page;
	}

	@Override
	public int makeSureOut(Integer[] ids) {
		int result = 0;
		Out out = null;
		Map<String, Integer> map = new HashMap<>();
		Stock stock = null;
		Date date = new Date();
		DetailList detailList = new DetailList();
		for (Integer id : ids) {
			map.clear();
			out = outDao.getById(id);
			map.put("productId", out.getProductId());
			map.put("warehouseId", out.getWarehouseId());
			stock = stockDao.getByProductAndWarehouse(map);
			try {
				if (stock == null) {
					stock = new Stock();
					stock.setProductId(out.getProductId());
					stock.setWarehouseId(out.getWarehouseId());
					stock.setStockNum(out.getRealbox());
					result = stockDao.add(stock);
					if (result == 0)
						break;
				} else {
					stock.setStockNum(stock.getStockNum() - out.getRealbox());
					result = stockDao.update(stock);
					if (result == 0)
						break;
				}
				// 确认out记录
				out.setOutStatusId(2);
				out.setDatetime(date);
				result = outDao.update(out);
				if (result == 0) {
					break;
				}
				// 更新商品状态
				if (stock.getStockNum() == 0) { // 当商品库存为0时, 将商品状态设为缺货
					out.getProduct().setProductStatusId(2);
					productDao.update(out.getProduct());
				}

				// 新增明细
				detailList.setOutId(out.getId());
				detailList.setDetailListTypeId(2);
				detailList.setMount(stock.getStockNum());
				result = detailListDao.add(detailList);
				if (result == 0)
					break;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public int updateNotNull(Out out) {
		return outDao.updateNotNull(out);
	}

	@Override
	public List<Out> findByMap(Map<String, Object> map) {
		return outDao.findByMap(map);
	}
}